package com.system.service.impl;

import com.system.mapper.*;
import com.system.pojo.*;
import com.system.service.AmountsService;
import com.system.vo.Amounts;
import com.system.vo.ResultVo;
import com.system.vo.TimeScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author yjymm
 * @date 2019-12-15 21:32
 */
@Service
public class AmountsServiceImpl implements AmountsService {

    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private InvoiceMapper invoiceMapper;

    @Autowired
    private ContractGoodsMapper cgm;


    @Override
    public ResultVo<Amounts> selectClientAmounts() {
        ResultVo<Amounts> resultVo = new ResultVo<Amounts>();
        Amounts amounts = new Amounts();
        List<Client> clients = clientMapper.selectClientAmounts();
        for (Client client : clients) {
            double amount = 0;
            List<Contract> contracts = client.getContracts();
            for (Contract contract : contracts) {
                amount += contract.getMoney();
            }
            client.setAmounts(amount);
        }
        amounts.setClients(clients);
        resultVo.setT(amounts);
        resultVo.setSelect(true);
        resultVo.setMessage("查询成功");
        return resultVo;
    }

    @Override
    public ResultVo<Amounts> selectProductAmounts() {
        ResultVo<Amounts> resultVo = new ResultVo<Amounts>();
        Amounts amounts = new Amounts();
        List<Product> products = productMapper.selectAll();
        for (Product product : products) {
            double amount = 0;
            Integer pid = product.getId();
            List<ContractGoods> cgs = null;
            cgs = cgm.selectByProductId(pid);
            for (ContractGoods cg : cgs) {
                amount += cg.getPurchasePrice()*cg.getPurchaseNum();
            }
            product.setAmounts(amount);
        }
        amounts.setProducts(products);
        resultVo.setT(amounts);
        resultVo.setSelect(true);
        resultVo.setMessage("查询成功");
        return resultVo;
    }

    @Override
    public ResultVo<Amounts> companyAllAmountsTime(Date time1, Date time2) {
        ResultVo<Amounts> resultVo = new ResultVo<Amounts>();
        Amounts amounts = new Amounts();
        TimeScope t = new TimeScope();
        t.setTime1(time1);
        t.setTime2(time2);
        List<Contract> contracts = contractMapper.selectAllWithTimeScope(t);
        double money = 0;
        for (Contract contract : contracts) {
            money += contract.getMoney();
        }
        amounts.setMoney(money);
        resultVo.setT(amounts);
        resultVo.setSelect(true);
        resultVo.setMessage("查询成功");
        return resultVo;
    }

    @Override
    public ResultVo<Amounts> sysUserAllAmounts(Integer uid) {
        ResultVo<Amounts> resultVo = new ResultVo<Amounts>();
        Amounts amounts = new Amounts();
        List<Contract> contracts = contractMapper.selectBySalerId(uid);
        double money = 0;
        for (Contract contract : contracts) {
            money += contract.getMoney();
        }
        amounts.setMoney(money);
        resultVo.setT(amounts);
        resultVo.setSelect(true);
        resultVo.setMessage("查询个人销售额成功");
        return resultVo;
    }

    @Override
    public ResultVo<Amounts> sysUserAmountsTime(Integer uid, Date time1, Date time2) {
        ResultVo<Amounts> resultVo = new ResultVo<Amounts>();
        TimeScope t = new TimeScope();
        t.setTime1(time1);
        t.setTime2(time2);
        t.setSalerId(uid);
        Amounts amounts = new Amounts();
        List<Contract> contracts = contractMapper.selectBySalerIdWithTimeScope(t);
        double money = 0;
        for (Contract contract : contracts) {
            money += contract.getMoney();
        }
        amounts.setMoney(money);
        resultVo.setT(amounts);
        resultVo.setSelect(true);
        resultVo.setMessage("查询个人销售额成功");
        return resultVo;
    }

    @Override
    public ResultVo<Amounts> companyAllAmounts() {
        ResultVo<Amounts> resultVo = new ResultVo<Amounts>();
        Amounts amounts = new Amounts();
        List<Contract> contracts = contractMapper.selectAll();
        double money = 0;
        for (Contract contract : contracts) {
            Double m = contract.getMoney();
            if (m!=0){
                money += m;
            }
        }
        amounts.setMoney(money);
        resultVo.setT(amounts);
        resultVo.setSelect(true);
        return resultVo;
    }

}
